<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;


class PersonalRizhi extends IndexController
{

	public function index()
	{
        $rizhiList = db('rizhi')->where('use_id',input('session.user.id'))->select();
        $this->assign('rizhiList', $rizhiList);

		return view('personal_rizhi/index');
	}

	
	public function readRizhi()
	{		

		Db::name('rizhi')
		->where('id', input('id'))
		->setInc('rz_read');

        $rizhiList = db('rizhi')->where('id',input('id'))->find();
        $this->assign('rizhiList', $rizhiList);	
        if(!$rizhiList){
        	return view('404/404');
        }

		$rzreply = Db::query('select u.nickname,u.icon,r.use_id,r.rz_content,r.rz_time,r.id from bbs_user u,bbs_rz_reply r where r.rz_id='.input('id').' and r.use_id=u.id order by r.rz_time desc');
		$this->assign('rzreply', $rzreply);	


		return view('personal_rizhi/read');
	}


	public function delRizhi()
	{
		$id = input('get.id');
		$result = db('rizhi')->delete($id);
		if($result){
			db('rz_reply')->where('rz_id',$id)->delete();
		}

		return json($result);

	}


	public function addRizhi()
	{

        return view('personal_rizhi/add');
	}

	public function doAddRizhi()
	{

		// error_reporting(E_ERROR|E_WARNING);
  // 		$content =  input();
	 // 	var_dump($content);exit;


		if (request()->isPost()) {
			$data = ['use_id' => input('session.user.id'), 'rz_content' => input('editorValue'), 'rz_time' => date("Y-m-d H:i:s"), 'rz_title' => input('title')];

			$validate = validate('Rizhi');
            if ($validate->check($data)) {
            	if(db('rizhi')->insert($data)){
    				//动态
					$dongtai = ['use_id' => input('session.user.id'),'dtime' => date("Y-m-d H:i:s"),'caozuo' => '新增了宠日志'.$data['rz_title']];
					db('dongtai')->insert($dongtai);

                	return $this->success('保存信息成功');
	            }else{
	                return $this->error('保存信息失败');
	            }

            }
            return $this->error($validate->getError());
 
        }
        return $this->fetch();

	}


	public function editRizhi()
	{

        $rizhiList = db('rizhi')->field('rz_title,rz_content,id')->where('id',input('id'))->find();
        $this->assign('rizhiList', $rizhiList);
        if(!$rizhiList){
        	return view('404/404');
        }

        return view('personal_rizhi/edit');
	}


	public function doEditRizhi()
	{

		if (request()->isPost()) {
			$data = [
                'id' => input('id'),
                'rz_title' => input('title'),
                'rz_content' => input('editorValue'),
            ];

			$validate = validate('Rizhi');
            if ($validate->check($data)) {
            	if(Db::name('rizhi')->where('id',input('id'))->update(['rz_title'=>$data['rz_title'],'rz_content'=>$data['rz_content'],'rz_time'=>date('Y-m-d H:i:s')])){
                	return $this->success('保存信息成功');
	            }else{
	                return $this->error('保存信息失败');
	            }

            }
            return $this->error($validate->getError());
 
        }
        return $this->fetch();
        	       
	}



	public function addreply()
	{	

		$user = db('rz_reply')->where('use_id',input('session.user.id'))->where('rz_id',input('rz_id'))->select();
		if($user){
			return json(0);
		}elseif(input('rz_content')==""){
			return json(0);
		}

		$data = ['rz_content' => input('rz_content'),'rz_id' => input('rz_id'),'use_id' => input('session.user.id'),'rz_time'=>date('Y-m-d H:i:s')];

		$result = Db::name('rz_reply')->insertGetId($data);
		$userlist = db('user')->where('id',input('session.user.id'))->find();
		$jsonlist = ['rz_content'=>input('rz_content'),'use_id' => input('session.user.id'),'rz_time'=>date('Y-m-d H:i:s'),'reply_id'=>$result,'icon'=>$userlist['icon'],'nickname'=>$userlist['nickname']];

		if($result){
			Db::name('rizhi')
			->where('id', input('rz_id'))
			->setInc('rz_pinglun');
			return json($jsonlist);
		}else{
			return json(0);
		}

	}


	public function delreply()
	{	
		$num = db('rz_reply')->where('id',input('id'))->find();
		$result = db('rz_reply')->delete(input('id'));

		
		if($result){
			Db::name('rizhi')
			->where('id', $num['rz_id'] )
			->setDec('rz_pinglun');
			return json(1);
		}else{
			return json(0);
		}

	}




	

	

}